package org.bingdianleil.stock.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.bingdianleil.config.DataSourceConfig;
import org.bingdianleil.util.DataSourceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class StockDao {

	private static String SqlString = "insert into dat_stock_history ( stock_id, stock_name, today_bg_price, last_close_price, price, today_top_price, today_lower_price, buy_one_now, sell_one_now, stock_quality, stock_amount, buy_one_quality, buy_one_price, buy_second_quality, buy_second_price, buy_third_quality, buy_third_price, buy_four_quality, buy_four_price, buy_five_quality, buy_five_price, sell_one_quality, sell_one_price, sell_second_quality, sell_second_price, sell_third_quality, sell_third_price, sell_four_quality, sell_four_price, sell_five_quality, sell_five_price, data_date, data_time,backup ) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

	@Autowired
	private DataSourceConfig dataSourceConfig;

	/**
	 * 当前日期数据插入
	 * 
	 * @param stock
	 * @return
	 */
	public boolean insertStock(String stock) {
		Connection connection = null;
		PreparedStatement pStatement = null;
		try {
			connection = dataSourceConfig.druidDataSource().getConnection();
			pStatement = connection.prepareStatement(SqlString);
			int i = 1;
			for (String value : stock.split(",")) {
				pStatement.setString(i++, value);
			}
			i = pStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DataSourceUtil.Release( pStatement,connection);
		}

		return true;
	}

	/**
	 * 当前日期数据插入
	 * @param stock
	 * @return
	 */
	public boolean insertStock(List<String> stockList) {
		Connection connection = null;
		PreparedStatement pStatement = null;
		try {
			connection = dataSourceConfig.druidDataSource().getConnection();
			connection.setAutoCommit(false);
			pStatement = connection.prepareStatement(SqlString);
			int i = 1;
			for (int j = 0; j < stockList.size(); j++) {
				i=1;
				for (String value : stockList.get(j).split(",")) {
					pStatement.setString(i++, value);
				}
				pStatement.addBatch();
			}
			pStatement.executeBatch();
			connection.commit();
			pStatement.clearBatch();
		} catch (SQLException e) {
			 
			System.out.println(e.getMessage());
		} finally {
			DataSourceUtil.Release( pStatement,connection);
		}

		return true;
	}

	/**
	 * 获取数据
	 * @param market
	 * @return
	 */
	public List<String> getStock(String market) {
		Connection connection = null;
		PreparedStatement pStatement = null;
		ResultSet rs = null;
		List<String> resultList = new ArrayList<String>();
		try {
			connection = dataSourceConfig.druidDataSource().getConnection();
			pStatement = connection.prepareStatement("select t.s_info_windcode from stock_base_info t where  t.s_info_code not  like 'A%'");
			rs = pStatement.executeQuery();
			while (rs.next()) {
				resultList.add(rs.getString(1));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DataSourceUtil.Release(rs,pStatement,connection);
		}

		return resultList;
	}

}
